package com.example.libraryapp.activity.personal;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;

import com.example.libraryapp.R;
import com.example.libraryapp.bean.User;
import com.example.libraryapp.bean.response.BaseResponse;
import com.example.libraryapp.dialog.personal.FavourDialog;
import com.example.libraryapp.dialog.personal.NameDialog;
import com.example.libraryapp.dialog.OnExitDialogListener;
import com.example.libraryapp.dialog.personal.OnPersonalListener;
import com.example.libraryapp.dialog.personal.SexDialog;
import com.example.libraryapp.network.base.BaseActivity;
import com.example.libraryapp.network.base.BasePresenter;
import com.example.libraryapp.network.net.RetrofitException;
import com.example.libraryapp.util.LoginUtil;
import com.example.libraryapp.util.SexUtil;
import com.example.libraryapp.util.ToastUtil;
import com.example.libraryapp.widget.ArrowView;
import com.example.libraryapp.widget.CnToolbar;

/**
 * @author 伍凯铭
 * @since 2020/7/23
 */
public class PersonalActivity extends BaseActivity<PersonalContract.View,PersonalPresenterIml>
        implements View.OnClickListener, OnPersonalListener,PersonalContract.View {

    private User mUser;

    private ArrowView mStudentId;

    private ArrowView mRealName;

    private ArrowView mSex;

    private ArrowView mAcademic;

    private ArrowView mFavor;

    private NameDialog mNameDialog;

    private SexDialog mSexDialog;

    private FavourDialog mFavourDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUser = LoginUtil.getNowUser(this);
        if(mUser == null) {
            finish();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    private void initData() {
        mStudentId.setRightText(mUser.getSno());
        mRealName.setRightText(mUser.getRealName());
        mSex.setRightText(SexUtil.getSex(this,mUser.getSex()));
        mAcademic.setRightText(mUser.getAcademic());
        mFavor.setRightText(mUser.getFavor());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                finish();
                break;
            case R.id.real_name:
                mNameDialog.setName(mUser.getRealName());
                mNameDialog.show();
                break;
            case R.id.sex:
                mSexDialog.init(mUser.getSex());
                mSexDialog.show();
                break;
            case R.id.academic:
                ToastUtil.showToast(this,"敬请期待");
                break;
            case R.id.favor:
                mFavourDialog.show();
                break;
        }
    }

    @Override
    protected void initView(View view) {
        mStudentId = view.findViewById(R.id.student_id);
        mRealName = view.findViewById(R.id.real_name);
        mSex = view.findViewById(R.id.sex);
        mAcademic = view.findViewById(R.id.academic);
        mFavor = view.findViewById(R.id.favor);
        mRealName.setOnClickListener(this);
        mSex.setOnClickListener(this);
        mAcademic.setOnClickListener(this);
        mFavor.setOnClickListener(this);
        mNameDialog = new NameDialog(this,this);
        mSexDialog = new SexDialog(this,this);
        mFavourDialog = new FavourDialog(this,this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_personal;
    }

    @Override
    protected PersonalPresenterIml initPresenter() {
        return new PersonalPresenterIml();
    }

    @Override
    protected void initToolBar(CnToolbar toolbar) {
        toolbar.setTitle(getResources().getString(R.string.personal_info));
        toolbar.setBackBtnOnClickListener(this);
    }

    @Override
    public void showNoNetwork(RetrofitException.ResponseThrowable error) {
        showContent();
        ToastUtil.showToast(this,error.message);
    }


    @Override
    public void onRealNameSure(String name) {
        showLoading();
        User user = new User();
        user.setUserId(mUser.getUserId());
        user.setRealName(name);
        mPresenter.updateUser(user);
    }

    @Override
    public void onSexSure(int sex) {
        if(mUser.getSex() != sex) {
            showLoading();
            User user = new User();
            user.setUserId(mUser.getUserId());
            user.setSex(sex);
            mPresenter.updateUser(user);
        }
    }

    @Override
    public void onFavourSure(String favour) {
        showLoading();
        User user = new User();
        user.setUserId(mUser.getUserId());
        user.setFavor(favour);
        mPresenter.updateUser(user);
    }

    @Override
    public void showUpdate(BaseResponse<User> response) {
        ToastUtil.showToast(this,response.getMessage());
        mUser = response.getResult();
        LoginUtil.login(this,mUser);
        initData();
    }
}
